import {
	Toast
} from 'react-vant';
const initialState = {
	checkall:(localStorage.getItem("checkAll") === "true" ? true : false) ||false, // 控制全选的变量
	arr: [],
	shoparr: JSON.parse(localStorage.getItem("shoparr")) ||[], // 购物车数组
	addressarr:[
		{
			tel:1311111111,
			name:"张三",
			address:"北京市昌平区"
		}
	]

}
// rxr  reducer 快捷键
export default (state = initialState, {
	type,
	payload
}) => {
  
	switch (type) {

		case "getlist":
			
			let newarr = [...state.arr]
			newarr = payload
			return {
				...state, arr: newarr
			}
			case "addShop":
				
				let newshoparr = [...state.shoparr]
				// findIndex 找下标，如果能从数组里找到这一项，返回这一项的下标，如果找不到，则返回-1
				let findIndex = state.shoparr.findIndex((item, index) => {
					return item.id === payload.id
				})
				if (findIndex !== -1) {
					Toast.fail("您已经加入够无车了")
				} else { // 没有加入过购物车
					newshoparr.push(payload)
				}
				localStorage.setItem("shoparr",JSON.stringify(newshoparr))
				return {
					...state,
					shoparr: newshoparr
				}
			case "addFn":
				let newaddshop = [...state.shoparr]
			   let newaddshop1 = newaddshop.map((item,index)=>{
					if (item.id === payload) {
						item.num ++
					}
					return item
				})
				localStorage.setItem("shoparr",JSON.stringify(newaddshop1))
				return {
					...state,
					shoparr:newaddshop1
				}
			case "delFn" :
				let newdelshop = [...state.shoparr]
				let newaddshop2 = newdelshop.map((item,index)=>{
					 if (item.id === payload) {
						 item.num --
						 if (item.num < 1) {
							 Toast.success("您不可以在减去了")
							item.num = 1
						 }
					 }
					 return item
				 })
				 localStorage.setItem("shoparr",JSON.stringify(newaddshop2))
				 return {
					 ...state,
					 shoparr:newaddshop2
				 }

			case "checkItem" :
				let checkshoparr =  [...state.shoparr]
				let newaddshop3 = checkshoparr.map((item,index)=>{
					if (item.id === payload) {
						item.flag = !item.flag
					}
					return item
				})
				let newcheckall =  newaddshop3.every((item)=>{ // every 返回一个布尔值， 如果返回的条件每一项都都为真，则返回真，如果有一项为假，则返回假
					return item.flag 
				})
				localStorage.setItem("checkAll",newcheckall.toString())
				localStorage.setItem("shoparr",JSON.stringify(newaddshop3))
				return {
					...state,
					shoparr:newaddshop3,
					checkall:newcheckall
				}
			case "checkallfn":

				state.checkall = !state.checkall
				let cheekAll = state.checkall;
				let cheakallarr = state.shoparr.map((item,index)=>{
					item.flag = cheekAll
					return item
				})
				localStorage.setItem("checkAll",cheekAll.toString())
				localStorage.setItem("shoparr",JSON.stringify(cheakallarr))
				return {
					...state,
					shoparr:cheakallarr,
					checkall:cheekAll
				}
		    case "delete1fn" :
				let del1shoparr = [...state.shoparr]
			    let newdel1shoparr =  del1shoparr.filter((item)=>item.id !== payload)
				localStorage.setItem("shoparr",JSON.stringify(newdel1shoparr))
				return {
					...state,
					shoparr:newdel1shoparr
				}
			case "addAaddress" :
				let newaddarr = [...state.addressarr]
				newaddarr.push(payload)
				return {
					...state,
					addressarr:newaddarr
				}
			case "deladdress" :
				let delarr = [...state.addressarr]
				delarr.splice(payload,1)
				return {
					...state,
					addressarr:delarr
				}
			default:
					return state
	}
}